package l1j.server.server.model.damage.physical;

import l1j.server.server.ActionCodes;
import l1j.server.server.WarTimeController;
import l1j.server.server.model.L1CastleLocation;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1NpcInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.model.Instance.L1PetInstance;
import l1j.server.server.model.Instance.L1SummonInstance;
import l1j.server.server.model.damage.L1DamageOnPc;
import l1j.server.server.model.damage.L1Dmg;
import l1j.server.server.model.damage.L1NpcDamage;
import l1j.server.server.serverpackets.S_DoActionGFX;
import l1j.server.server.utils.Random;

public class L1PhysicalDamage_Npc_Pc extends L1Dmg {
	private L1NpcInstance _attacker;

	private L1PcInstance _target;

	private L1NpcDamage _npcDamage;

	public L1PhysicalDamage_Npc_Pc(L1NpcInstance attacker, L1PcInstance target) {
		_attacker = attacker;
		_target = target;
		_npcDamage = new L1NpcDamage(attacker, target, this);
	}

	@Override
	public void calcDmg() {
		_npcDamage.calcNpcDmg();
		L1DamageOnPc dop = new L1DamageOnPc(this, _target);
		dop.calcDamageReductionByAc();
		dop.calcDamageOnPc();
		_npcDamage.isWeaponBreaked();
		isNowWar();
		_npcDamage.addNpcPoisonAttack();
	}

	@Override
	public void hurtTarget(boolean actionHurt) {
		commitAttacker(_attacker);
		commitTarget(_target);
		_target.receiveDamage(_attacker, getValue());
		if (getValue() > 0 && actionHurt) {
			_target.sendPackets(new S_DoActionGFX(_target.getId(),
					ActionCodes.ACTION_Damage));
			_target.broadcastPacket(new S_DoActionGFX(_target.getId(),
					ActionCodes.ACTION_Damage));
		}
	}
	
	private void isNowWar() {
		// ペット、サモンからプレイヤーに攻撃
		boolean isNowWar = false;
		int castleId = L1CastleLocation.getCastleIdByArea(_target);
		if (castleId > 0) {
			isNowWar = WarTimeController.getInstance().isNowWar(castleId);
		}
		if (!isNowWar) {
			if (_attacker instanceof L1PetInstance) {
				div(8);
			} else if (_attacker instanceof L1SummonInstance) {
				L1SummonInstance summon = (L1SummonInstance) _attacker;
				if (summon.isExsistMaster()) {
					div(8);
				}
			}
		}
	}

}
